import AppBradge from './appBradge'
export default {
  getToken () {
    // 获取到token
    return new Promise((resolve, reject) => {
      // 正式
      if (!window.WebViewJavascriptBridge) {
        AppBradge.registerBridge()
      }
      if (sessionStorage.getItem('userMsg') && sessionStorage.getItem('userMsg') !== '') {
        // 从本地获取token
        // console.log('从本地获取token')
        let userMsg = JSON.parse(sessionStorage.getItem('userMsg'))
        resolve(userMsg)
      } else {
        // 从Android获取token
        // console.log('从Android获取token')
        AppBradge.getUserMsg((dataobj) => {
          sessionStorage.setItem('userMsg', dataobj)
          resolve(JSON.parse(dataobj))
        })
      }
    })
  },
  getTokenAsync () {
    if (!window.WebViewJavascriptBridge) {
      AppBradge.registerBridge()
    }
    if (sessionStorage.getItem('userMsg') && sessionStorage.getItem('userMsg') !== '') {
      // 从本地获取token
      // console.log('从本地获取token')
      let userMsg = JSON.parse(sessionStorage.getItem('userMsg'))
      return userMsg
    } else {
      // 从Android获取token
      // console.log('从Android获取token')
      AppBradge.getUserMsg((dataobj) => {
        sessionStorage.setItem('userMsg', dataobj)
        return JSON.parse(dataobj)
      })
    }
  }
}
